package com.registration.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.registration.Annotation.OperateLog;
import com.registration.bean.PCUserBean;
import com.registration.service.MenuService;
import com.registration.service.PCUserService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/pcUser")
@RestController
@CrossOrigin
public class PCUserController {

    @Autowired
    private PCUserService pcUserService;

    @Autowired
    MenuService menuService;

    @GetMapping("/login")
    @OperateLog("后台登录")
    public JsonResponse login(String username, String password) {
        JsonResponse json = new JsonResponse();
        try {

            PCUserBean result = pcUserService.login(username, password);
            if (result != null) {
                json.setCode(0);
                json.setMsg("登录成功");
                json.setData(result);
                json.setSaTokenInfo(StpUtil.getTokenInfo());
                System.out.println(StpUtil.getTokenInfo());
            } else {
                json.setCode(1);
                json.setMsg("用户名或密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setCode(0);
            json.setMsg("查询数据异常");
        }
        return json;
    }

    @GetMapping("/adminInfo")
    @OperateLog("管理员信息")
    public JsonResponse adminInfo() {
        JsonResponse json = new JsonResponse();
        try {
            List<PCUserBean> result = pcUserService.adminInfo();
            if (result != null) {
                json.setCode(1);
                json.setMsg("查询成功");
                json.setData(result);
            } else {
                json.setCode(0);
                json.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setCode(0);
            json.setMsg("查询数据异常");
        }
        return json;
    }




                @PostMapping("/insertPCUser")
                @OperateLog("添加用户")
                @SaCheckPermission("sys:home:user:add")
                public JsonResponse insertPCUser (@RequestBody PCUserBean pcUserBean){

                    try {
                        return JsonResponse.success(pcUserService.insert(pcUserBean));
                    } catch (Exception e) {
                        return JsonResponse.fail(e.getMessage());
                    }

                }

                @DeleteMapping("/deletePCUser/{pc_user_id}")
                @OperateLog("删除用户")
                @SaCheckPermission("sys:home:user:delete")
                public JsonResponse deletePCUser (@PathVariable Integer pc_user_id){

                    try {
                        return JsonResponse.success(pcUserService.delete(pc_user_id));
                    } catch (Exception e) {
                        return JsonResponse.fail("系统异常");
                    }

                }

                @PutMapping("/updatePCUser")
                @OperateLog("更新用户")
                @SaCheckPermission("sys:home:user:update")
                public JsonResponse updatePCUser (@RequestBody PCUserBean pcUserBean){


                    try {
                        return JsonResponse.success(pcUserService.updatePCUser(pcUserBean));
                    } catch (Exception e) {
                        return JsonResponse.fail("系统异常");
                    }

                }


                @GetMapping("/getPCUserList/{pageNum}/{pageSize}")
                @OperateLog("查看用户列表")
                @SaCheckPermission("sys:home:user:view")
                public JsonResponse getPCUserList (@PathVariable Integer pageNum, @PathVariable Integer
                pageSize, PCUserBean pcUserBean){


                    try {
                        return JsonResponse.success(pcUserService.getPCUserList(pageNum, pageSize, pcUserBean));
                    } catch (Exception e) {
                        e.printStackTrace();
                        return JsonResponse.fail("系统异常");
                    }

                }


                @GetMapping("getMenuList")
                public JsonResponse getMenuList (Integer pc_user_id){

                    try {
                        return JsonResponse.success(menuService.getMenuListByPCUserId(pc_user_id));
                    } catch (Exception e) {
                        return JsonResponse.fail("系统异常");
                    }

                }
            }

